package it.heima.axios1.controler;


import it.heima.axios1.pojo.Result;
import it.heima.axios1.pojo.User;
import it.heima.axios1.service.Login;
import it.heima.axios1.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class loginController {
    @Autowired
    private Login login;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {

        log.info(user.toString());
        User user1 = login.login(user);
        log.info("查询成功");
        if (user1 != null){
            Map<String, Object> claims = new HashMap<>();
            //claims.put("id", user1.getId());
            claims.put("username", user1.getUsername());
            claims.put("password", user1.getPassword());
            String token = JwtUtils.generateJwt(claims);
            log.info("token:{}", token);
            return Result.success(token);
        }

        return Result.error("没有该账号或者密码");
    }

    @GetMapping("/zz")
    public String register() {
        return "success";
    }


}
